package com.lagou.service;

import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import com.lagou.utils.Constant;
import com.lagou.bean.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.concurrent.Callable;
import java.util.concurrent.TimeUnit;


/**
 * @author senyang
 */
@Component
public class GuavaCacheService {

    @Autowired
    RedisCacheService redisCacheService;

    /**
     * CacheLoader的方式创建
     */
    private LoadingCache<String, Object> cache = null;

    /**
     * 读取缓存数据 如果没有则回调源数据并(自动)写入缓存
     */
    public Object getHotPosGuavaCache() {
        startHotPosGuavaCache();
        try{
            Object obj = cache.get(Constant.HOT_POSITION_KEY, new Callable<Object>() {
                @Override //回调方法用于读源并写入缓存
                public Object call() throws Exception {
                    System.out.println(">>>>>>>>>>>>>>>   guava cache 未获取到热门职位缓存，开始去Redis获取热门职位缓存   >>>>>>>>>>>>>>>");
                    //读源
                    Result all = redisCacheService.getHotPosRedisCache();
                    //写回缓存
                    cache.put(Constant.HOT_POSITION_KEY, all);
                    System.out.println(">>>>>>>>>>>>>>>   开始设置  guava cache 热门职位缓存  >>>>>>>>>>>>>>>");
                    return all;
                }
            });
            System.out.println(">>>>>>>>>>>>>>>   guava cache 获取到了热门职位缓存，直接返回了 >>>>>>>>>>>>>>>>>");
            return obj;
        }catch (Exception e){
            System.out.println("获取guava cache 出错：" + e.getMessage());
            e.printStackTrace();
            return Result.error();
        }
    }


    public void removeHotPosGuavaCache(){
        try {
            cache.invalidate(Constant.HOT_POSITION_KEY);
        }catch (Exception e){
            System.err.println("删除guava 缓存发生错误："+ e.getMessage());
        }
        redisCacheService.removeRedisCache(Constant.HOT_POSITION_KEY);
    }


    public LoadingCache<String, Object> startHotPosGuavaCache() {
        if(cache == null){
            cache = CacheBuilder.newBuilder()
                    /*
                         先初始化缓存，加载数据库
                     */
                    //等同于expire  ttl  缓存中对象的生命周期就是3秒
                    .maximumSize(3).expireAfterWrite(Constant.HOT_POSITION_KEY_EXPIRE, TimeUnit.SECONDS)
                    .build(new CacheLoader<String, Object>() {
                        //读取数据源
                        @Override
                        public Object load(String key) throws Exception {
                            System.out.println(">>>>>>>>>>>>>>>   guava cache 热门职位缓存为空，初始化数据，开始去Redis获取热门职位缓存   >>>>>>>>>>>>>>>");
                            key = Constant.HOT_POSITION_KEY;
                            Result all = redisCacheService.getHotPosRedisCache();
                            System.out.println(">>>>>>>>>>>>>>>   开始初始化  guava cache 热门职位缓存  >>>>>>>>>>>>>>>");
                            return all;
                        }
                    });
        }
        return cache;
    }

}
